package com.dlg.user.rpc.provider.user;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dlg.commons.constant.Constants;
import com.dlg.commons.constant.DigitConstant;
import com.dlg.commons.constant.MqKeyConstants;
import com.dlg.commons.constant.RedisKeyConstants;
import com.dlg.commons.dto.VerifyCode;
import com.dlg.commons.enums.AgentRateEnum;
import com.dlg.commons.enums.SourceEnum;
import com.dlg.commons.enums.UserType;
import com.dlg.commons.enums.VerifyCodeEnum;
import com.dlg.commons.exception.DataSaveException;
import com.dlg.commons.exception.DataUpdateException;
import com.dlg.commons.util.Utils;
import com.dlg.sms.rpc.api.service.ISmsRpcService;
import com.dlg.user.dao.mysql.mappers.user.OjUserEnterpriseAgentAttributeMapper;
import com.dlg.user.dao.mysql.mappers.user.OjUserEnterpriseAttributeMapper;
import com.dlg.user.dao.mysql.mappers.user.OjUserMapper;
import com.dlg.user.dao.mysql.mappers.user.OjUserPersonalAttributeMapper;
import com.dlg.user.dao.redis.CacheService;
import com.dlg.user.domain.system.OjDictionary;
import com.dlg.user.domain.user.OjUser;
import com.dlg.user.domain.user.OjUserEnterpriseAgentAttribute;
import com.dlg.user.domain.user.OjUserEnterpriseAttribute;
import com.dlg.user.domain.user.OjUserPersonalAttribute;
import com.dlg.user.rabbitmq.sender.IMqSender;
import com.dlg.user.rpc.api.system.IDictionaryRpcService;
import com.dlg.user.rpc.api.user.IUserRelationshipRpcService;
import com.dlg.user.rpc.api.user.IUserRpcService;
import com.dlg.user.rpc.api.vo.user.*;
import org.apache.shiro.authc.credential.PasswordMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;


/**
 * 用户相关rpc服务
 *
 * @author lhh@gongren.com
 * @since 2017年3月30日 上午9:32:37
 */
@Service(interfaceName="com.dlg.user.rpc.api.user.IUserRpcService",retries = 10,timeout = 60000)
public class UserRpcServiceImpl implements IUserRpcService {

    @Reference
    private IUserRelationshipRpcService userRelationshipRpcService;

    @Autowired
    private CacheService cacheService;

    @Reference
    private ISmsRpcService smsRpcService;

    @Autowired
    private IMqSender mqSender;

    @Value(value = "${spread.login.url}")
    private String spreadLoginUrl;

    @Autowired
    private IDictionaryRpcService dictionaryRpcService;

    @Autowired
    private OjUserMapper userMapper;

    @Autowired
    private OjUserEnterpriseAttributeMapper userEnterpriseAttributeMapper;

    @Autowired
    private OjUserPersonalAttributeMapper userPersonalAttributeMapper;

    @Autowired
    private OjUserEnterpriseAgentAttributeMapper userEnterpriseAgentAttributeMapper;

    @Autowired
    PasswordMatcher credentialsMatcher;

    @Override
    public BigInteger register(UserRegisterRpcVo userRpcRegVo, String userType) {
        // 检查当前手机号和用户类型是否已经注册
        if (cacheService.existsKey(RedisKeyConstants.getUserRegisteredFlag(userRpcRegVo.getPhone(), userType))){
            throw new DataSaveException("当前手机号已注册");
        }
        //注册标志,用于判断是否已经注册
        cacheService.putObjectPermanent(RedisKeyConstants.getUserRegisteredFlag(userRpcRegVo.getPhone(), userType), true);

        BigInteger id = Utils.id();
        userRpcRegVo.setId(id);
        String msg = userRpcRegVo.validate();
        if (msg!=null){
            throw new DataSaveException(msg);
        }

        try {
            //单独写的dubbo服务 校验手机号和验证码是否匹配
            if(!"offline".equals(userRpcRegVo.getSource())){
                // 校验手机号和验证码是否匹配
                VerifyCode verifyCodeVo = new VerifyCode();
                verifyCodeVo.setPhone(userRpcRegVo.getPhone());
                verifyCodeVo.setVerifyCode(userRpcRegVo.getVaildCode());
                verifyCodeVo.setSource(SourceEnum.ODD_JOB);
                VerifyCodeEnum verifyCodeEnum = smsRpcService.checkCodeAndDelete(verifyCodeVo);
                if (null == verifyCodeEnum || VerifyCodeEnum.SUCCESS != verifyCodeEnum) {
                    throw new DataSaveException("验证码校验不通过，错误原因" + verifyCodeEnum.getValue());
                }
            }
            //查询距离
            List<OjDictionary> dictionaries = dictionaryRpcService.findByGroupCode("jd.scope", (short) 1);
            String distance = null;
            if (!CollectionUtils.isEmpty(dictionaries)) {
                distance = dictionaries.get(0).getDataCode();
            }
            userRpcRegVo.getEnterpriseUserRpcVo().setId(Utils.id());
            userRpcRegVo.getEnterpriseUserRpcVo().setUserId(id);
            userRpcRegVo.getEnterpriseUserRpcVo().setContact(userRpcRegVo.getUsername());
            userRpcRegVo.getEnterpriseUserRpcVo().setContactTelephone(userRpcRegVo.getPhone());
            userRpcRegVo.getEnterpriseUserRpcVo().setIsAgent(DigitConstant.SHORT_ZERO);
            if (null == userRpcRegVo.getEnterpriseUserRpcVo().getDistance()) {
                userRpcRegVo.getEnterpriseUserRpcVo().setDistance(distance);
            }

            userRpcRegVo.getPersonalUserRpcVo().setId(Utils.id());
            userRpcRegVo.getPersonalUserRpcVo().setUserId(id);
            if (null == userRpcRegVo.getPersonalUserRpcVo().getDistance()) {
                userRpcRegVo.getPersonalUserRpcVo().setDistance(distance);
            }

            //保存用户,先放到redis，再通过mq持久化
            //放redis，表明注册成功
            cacheService.putObjectPermanent(RedisKeyConstants.getUserLoginKey(userRpcRegVo.getPhone(), userType), userRpcRegVo);
        }catch (Exception e){
            cacheService.remove(RedisKeyConstants.getUserRegisteredFlag(userRpcRegVo.getPhone(), userType));
            cacheService.remove(RedisKeyConstants.getUserLoginKey(userRpcRegVo.getPhone(), userType));
            throw e;
        }

        //持久化用户信息到mysql
        mqSender.send(MqKeyConstants.register, userRpcRegVo);
        //todo 同步帐号到橙子中心-mq,还没有消费者（上海写）
        mqSender.send(MqKeyConstants.syncAccountToOrange, id.toString()+"-"+SourceEnum.ODD_JOB);

        return id;
    }

    @Override
    public void updateUserPassword(String phone, String userType, String newPassword, String oldPassword) {
        String loginKey = RedisKeyConstants.getUserLoginKey(phone, userType);
        if (!cacheService.existsKey(loginKey)){
            throw new DataUpdateException("用户不存在");
        }
        UserRpcVo user = cacheService.getObject(loginKey,UserRegisterRpcVo.class);
        if (oldPassword == null || !oldPassword.matches("[\\w\\W]{6,18}") || newPassword == null
                || !newPassword.matches("[\\w\\W]{6,18}")) {
            throw new DataUpdateException("密码长度应为6-18位");
        }
        boolean flag = credentialsMatcher.getPasswordService().passwordsMatch(oldPassword, user.getPassword());
        if (!flag) {
            throw new DataUpdateException("原始密码不正确");
        }
        String password = Utils.encryptPassword(newPassword, credentialsMatcher.getPasswordService());
        user.setPassword(password);
        cacheService.putObjectPermanent(loginKey, user);

        OjUser ojUser = new OjUser();
        Utils.copyNotNullObject(user, ojUser);
        mqSender.send(MqKeyConstants.updateUser,ojUser);
    }

    @Override
    public BigInteger addEnterpriseUser(OjUserEnterpriseAttribute enterpriseAttribute) {
        if (null == enterpriseAttribute) {
            throw new DataSaveException("要保存的企业用户信息为空");
        }
        if (null == enterpriseAttribute.getUserId()) {
            throw new DataSaveException("要保存的企业用户UserId为空");
        }
        if (userEnterpriseAttributeMapper.findOneByUserId(enterpriseAttribute.getUserId()) != null) {
            throw new DataSaveException("该用户已经存在企业信息");
        }
        if (enterpriseAttribute.getId()==null){
            BigInteger id = Utils.id();
            enterpriseAttribute.setId(id);
        }

        userEnterpriseAttributeMapper.save(enterpriseAttribute);
        return enterpriseAttribute.getId();
    }

    @Override
    public BigInteger addPersonalUser(OjUserPersonalAttribute ojUserPersonalAttribute) {
        if (null == ojUserPersonalAttribute) {
            throw new DataSaveException("要保存的个人用户信息为空");
        }
        if (null == ojUserPersonalAttribute.getUserId()) {
            throw new DataSaveException("要保存的个人用户UserId为空");
        }
        BigInteger userId = ojUserPersonalAttribute.getUserId();
        if (userPersonalAttributeMapper.findOneByUserId(userId) != null) {
            throw new DataSaveException("该用户已经存在个人信息");
        }
        if (ojUserPersonalAttribute.getId()==null) {
            BigInteger id = Utils.id();
            ojUserPersonalAttribute.setId(id);
        }
        userPersonalAttributeMapper.save(ojUserPersonalAttribute);
        return ojUserPersonalAttribute.getId();
    }

    @Override
    public UserRpcVo queryUserDetail(String phone, UserType userType) {
        UserRpcVo userRpcVo = new UserRpcVo();
        if(org.apache.commons.lang3.StringUtils.isEmpty(phone) || userType==null){
            return null;
        }
        String type = UserType.getNumOfStr(userType);
        String loginKey = RedisKeyConstants.getUserLoginKey(phone, type);
        if (!cacheService.existsKey(loginKey)){//如何缓存中不存在就从数据库取

            OjUser user = userMapper.findByPhoneAndType(phone, userType);
            if (user == null) {
                return null;
            }
            Utils.copyNotNullObject(user, userRpcVo);
            if (null == user.getType()) {
                return userRpcVo;
            }
            if (user.getType().equals(UserType.PERSONAL)) {
                OjUserPersonalAttribute userPersonalAttribute = userPersonalAttributeMapper.findOneByUserId(user.getId());
                if (null != userPersonalAttribute) {
                    PersonalUserAttributeRpcVo personalUserAttributeRpcVo = new PersonalUserAttributeRpcVo();
                    Utils.copyNotNullObject(userPersonalAttribute, personalUserAttributeRpcVo);
                    userRpcVo.setPersonalUserAttributeRpcVo(personalUserAttributeRpcVo);
                }
            } else if (user.getType().equals(UserType.ENTERPRISE)) {
                OjUserEnterpriseAttribute userEnterpriseAttribute = userEnterpriseAttributeMapper.findOneByUserId(user.getId());
                if (userEnterpriseAttribute != null) {
                    EnterpriseUserAttributeRpcVo enterpriseUserAttributeRpcVo = new EnterpriseUserAttributeRpcVo();
                    Utils.copyNotNullObject(userEnterpriseAttribute,enterpriseUserAttributeRpcVo);
                    userRpcVo.setEnterpriseUserAttributeRpcVo(enterpriseUserAttributeRpcVo);
                }
            }else if (user.getType().equals(UserType.AGENT)) {
                OjUserEnterpriseAgentAttribute userEnterpriseAgentAttribute = userEnterpriseAgentAttributeMapper.findOneByUserId(user.getId());
                if (userEnterpriseAgentAttribute != null) {
                    EnterpriseAgentAttributeRpcVo enterpriseUserAgentAttributeRpcVo = new EnterpriseAgentAttributeRpcVo();
                    Utils.copyNotNullObject(userEnterpriseAgentAttribute,enterpriseUserAgentAttributeRpcVo);
                    userRpcVo.setEnterpriseAgentAttributeRpcVo(enterpriseUserAgentAttributeRpcVo);
                }
            }
        }else{//从缓存取
            userRpcVo = cacheService.getObject(loginKey,UserRegisterRpcVo.class);
        }

        return userRpcVo;
    }

    @Override
    public void updatePersonalUserByUserId(PersonalUserAttributeRpcVo attributeRpcVo, String phone, String userType) {
        if (!UserType.getTypeByStr(userType).equals(UserType.PERSONAL)){
            throw new DataUpdateException("该用户不是个人用户");
        }
        String loginKey = RedisKeyConstants.getUserLoginKey(phone, userType);
        if (!cacheService.existsKey(loginKey)){
            throw new DataUpdateException("用户不存在");
        }
        UserRegisterRpcVo registerRpcVo = cacheService.getObject(loginKey, UserRegisterRpcVo.class);
        PersonalUserAttributeRpcVo personal = registerRpcVo.getPersonalUserRpcVo();
        if (personal==null){
            throw new DataUpdateException("用户不存在");
        }
        attributeRpcVo.setUserId(registerRpcVo.getId());
        attributeRpcVo.setModifyUserId(registerRpcVo.getId());
        attributeRpcVo.setVersion(attributeRpcVo.getVersion()==null ? 0 : attributeRpcVo.getVersion()+1);

        if (attributeRpcVo.getJoinCount()!=null){
            Integer joinCount = personal.getJoinCount();
            joinCount = joinCount==null ? 0 : joinCount;
            attributeRpcVo.setJoinCount(joinCount+attributeRpcVo.getJoinCount());
        }
        if (attributeRpcVo.getCancelCount()!=null){
            Integer cancelCount = personal.getCancelCount();
            cancelCount = cancelCount==null ? 0 : cancelCount;
            attributeRpcVo.setCancelCount(cancelCount+attributeRpcVo.getCancelCount());
        }
        if (attributeRpcVo.getReleaseCount()!=null){
            Integer releaseCount = personal.getReleaseCount();
            releaseCount = releaseCount==null ? 0 : releaseCount;
            attributeRpcVo.setReleaseCount(releaseCount+attributeRpcVo.getReleaseCount());
        }
        if (attributeRpcVo.getLateCount()!=null){
            Integer lateCount = personal.getLateCount();
            lateCount = lateCount==null ? 0 : lateCount;
            attributeRpcVo.setLateCount(lateCount+attributeRpcVo.getLateCount());
        }
        if (attributeRpcVo.getScoreCount()!=null){
            Double scoreCount = personal.getScoreCount();
            scoreCount = scoreCount==null ? 0 : scoreCount;
            attributeRpcVo.setScoreCount(scoreCount+attributeRpcVo.getScoreCount());
        }
        if (attributeRpcVo.getCreditCount()!=null){
            Double creditCount = personal.getCreditCount();
            creditCount = creditCount==null ? 0 : creditCount;
            attributeRpcVo.setCreditCount(creditCount+attributeRpcVo.getCreditCount());
        }

        attributeRpcVo.setId(personal.getId());
        registerRpcVo.setPersonalUserRpcVo(attributeRpcVo);
        cacheService.putObjectPermanent(loginKey, registerRpcVo);

        //mq 持久化
        OjUserPersonalAttribute personalAttribute = new OjUserPersonalAttribute();
        Utils.copyNotNullObject(attributeRpcVo, personalAttribute);
        mqSender.send(MqKeyConstants.updatePersonalAttribute, personalAttribute);

    }

    @Override
    public Integer getJobCountByUserId(BigInteger userId) {
        if (userId==null){
            return 0;
        }
        return userMapper.getJobCountByUserId(userId);
    }

    @Override
    public BigInteger addEnterpriseAgent(EnterpriseAgentAttributeRpcVo enterpriseAgentAttributeRpcVo, String phone, String userType) {
        if (!"2".equals(userType)){
            throw new DataSaveException("该用户不是企业用户");
        }
        enterpriseAgentAttributeRpcVo.setId(Utils.id());
        String loginKey = RedisKeyConstants.getUserLoginKey(phone, userType);

        if (!cacheService.existsKey(loginKey)){
            throw new DataSaveException("用户不存在");
        }

        UserRegisterRpcVo loginUserVo = cacheService.getObject(loginKey, UserRegisterRpcVo.class);

        Short agentLevel = enterpriseAgentAttributeRpcVo.getAgentLevel();
        if (agentLevel == null || agentLevel > 4 || agentLevel < 1) {
            throw new DataSaveException("代理商类型不正确");
        }
        if (!Short.valueOf("2").equals(loginUserVo.getAuditStatus())) {
            throw new DataSaveException("当前用户不允许设为代理商");
        }
        if (loginUserVo.getEnterpriseUserRpcVo().getId()==null){
            throw new DataSaveException("企业用户id为空");
        }
        Short taxRate = 0;
        switch (agentLevel) {
            case 1:
                taxRate = AgentRateEnum.PROVINCE_AGENT.getValue();
                break;
            case 2:
                taxRate = AgentRateEnum.CITY_AGENT.getValue();
                break;
            case 3:
                taxRate = AgentRateEnum.AREA_AGENT.getValue();
                break;
            case 4:
                taxRate = AgentRateEnum.VILLAGE_AGENT.getValue();
                break;
            default:
                break;
        }
        String spreadRegisterUrl = spreadLoginUrl + "?inviter="
                + loginUserVo.getEnterpriseUserRpcVo().getId() + "&inviteName="
                + loginUserVo.getEnterpriseUserRpcVo().getName();
        enterpriseAgentAttributeRpcVo.setId(Utils.id());
        enterpriseAgentAttributeRpcVo.setSpreadRegisterUrl(spreadRegisterUrl);
        enterpriseAgentAttributeRpcVo.setUserId(loginUserVo.getId());
        enterpriseAgentAttributeRpcVo.setModifyUserId(loginUserVo.getId());
        enterpriseAgentAttributeRpcVo.setUserEnterpriseAttributeId(loginUserVo.getEnterpriseUserRpcVo().getId());
        enterpriseAgentAttributeRpcVo.setAgentLevel(agentLevel);
        enterpriseAgentAttributeRpcVo.setAgentTaxRate(taxRate);
        enterpriseAgentAttributeRpcVo.setAgentStatus(Short.valueOf(Constants.AGENT_STATUS_OK));
        enterpriseAgentAttributeRpcVo.setForwardCount(0);
        enterpriseAgentAttributeRpcVo.setAgentCount(0);
        enterpriseAgentAttributeRpcVo.setPersonalAgentCount(0);
        enterpriseAgentAttributeRpcVo.setEnterpriseAgentCount(0);
        enterpriseAgentAttributeRpcVo.setCreateTime(new Date());
        enterpriseAgentAttributeRpcVo.setCreateUserId(loginUserVo.getId());

        loginUserVo.getEnterpriseUserRpcVo().setEnterpriseAgentAttributeRpcVo(enterpriseAgentAttributeRpcVo);
        cacheService.putObjectPermanent(loginKey, loginUserVo);

        OjUserEnterpriseAgentAttribute agentAttribute = new OjUserEnterpriseAgentAttribute();
        Utils.copyNotNullObject(enterpriseAgentAttributeRpcVo,agentAttribute);
        //mq 持久化
        mqSender.send(MqKeyConstants.insertEnterpriseAgent, agentAttribute);
        return enterpriseAgentAttributeRpcVo.getId();
    }

    @Transactional
    @Override
    public void updateEnterpriseUserByUserId(EnterpriseUserAttributeRpcVo attributeRpcVo, String phone, String userType) {

        if (!"2".equals(userType)){
            throw new DataUpdateException("该用户不是企业用户");
        }
        String loginKey = RedisKeyConstants.getUserLoginKey(phone, userType);
        if (!cacheService.existsKey(loginKey)){
            throw new DataUpdateException("用户不存在");
        }
        UserRegisterRpcVo userRpcVo = cacheService.getObject(loginKey,UserRegisterRpcVo.class);
        EnterpriseUserAttributeRpcVo enterprise = userRpcVo.getEnterpriseUserRpcVo();
        if (enterprise==null){
            throw new DataUpdateException("企业不存在");
        }
        //是否需要修改用户的认证状态和名称
        boolean needUpdateUser = (attributeRpcVo.getAuditStatus()!=null && !attributeRpcVo.getAuditStatus().equals(userRpcVo.getAuditStatus()))
                || (org.apache.commons.lang3.StringUtils.isNotEmpty(attributeRpcVo.getName()) && !attributeRpcVo.getName().equals(userRpcVo.getUsername()));

        attributeRpcVo.setId(enterprise.getId());
        attributeRpcVo.setUserId(userRpcVo.getId());
        attributeRpcVo.setModifyUserId(userRpcVo.getId());
        attributeRpcVo.setVersion(attributeRpcVo.getVersion()==null ? 0 : attributeRpcVo.getVersion()+1);

        if (attributeRpcVo.getReleaseCount()!=null){
            Integer oldCount = enterprise.getReleaseCount();
            oldCount = oldCount==null ? 0 : oldCount;
            attributeRpcVo.setReleaseCount(oldCount+attributeRpcVo.getReleaseCount());
        }
        if (attributeRpcVo.getScoreCount()!=null){
            Double oldCount = enterprise.getScoreCount();
            oldCount = oldCount==null ? 0 : oldCount;
            attributeRpcVo.setScoreCount(oldCount+attributeRpcVo.getScoreCount());
        }
        if (attributeRpcVo.getCreditCount()!=null){
            Double oldCount = enterprise.getCreditCount();
            oldCount = oldCount==null ? 0 : oldCount;
            attributeRpcVo.setCreditCount(oldCount+attributeRpcVo.getCreditCount());
        }

        if (needUpdateUser) {
            userRpcVo.setUsername(attributeRpcVo.getName());
            userRpcVo.setAuditStatus(attributeRpcVo.getAuditStatus());
            userRpcVo.setVersion(userRpcVo.getVersion()==null ? 0 : userRpcVo.getVersion()+1);
        }
        EnterpriseAgentAttributeRpcVo enterpriseAgentAttributeRpcVo = enterprise.getEnterpriseAgentAttributeRpcVo();
        userRpcVo.setEnterpriseUserRpcVo(attributeRpcVo);
        enterprise.setEnterpriseAgentAttributeRpcVo(enterpriseAgentAttributeRpcVo);
        cacheService.putObjectPermanent(loginKey, userRpcVo);

        if (needUpdateUser) {
            OjUser ojUser = new OjUser();
            ojUser.setId(userRpcVo.getId());
            ojUser.setUsername(attributeRpcVo.getName());
            ojUser.setAuditStatus(attributeRpcVo.getAuditStatus());
            ojUser.setVersion(userRpcVo.getVersion()==null ? 0 : userRpcVo.getVersion()+1);
            //企业修改为已认证
            mqSender.send(MqKeyConstants.updateUser, ojUser);
        }
        OjUserEnterpriseAttribute ojUserEnterpriseAttribute = new OjUserEnterpriseAttribute();
        Utils.copyNotNullObject(attributeRpcVo, ojUserEnterpriseAttribute);
        //mq 持久化
        mqSender.send(MqKeyConstants.updateEnterpriseAttribute, ojUserEnterpriseAttribute);

    }

    @Override
    public void updateEnterpriseAgentByUserId(EnterpriseAgentAttributeRpcVo attributeRpcVo, String phone, String userType) {

        String loginKey = RedisKeyConstants.getUserLoginKey(phone, userType);
        if (!cacheService.existsKey(loginKey)){
            throw new DataUpdateException("用户不存在");
        }
        UserRegisterRpcVo registerRpcVo = cacheService.getObject(loginKey, UserRegisterRpcVo.class);
        if (registerRpcVo.getEnterpriseUserRpcVo()==null || registerRpcVo.getEnterpriseUserRpcVo().getEnterpriseAgentAttributeRpcVo()==null){
            throw new DataUpdateException("代理商不存在");
        }
        EnterpriseAgentAttributeRpcVo agent = registerRpcVo.getEnterpriseUserRpcVo().getEnterpriseAgentAttributeRpcVo();
        attributeRpcVo.setId(agent.getId());
        attributeRpcVo.setUserId(registerRpcVo.getId());
        attributeRpcVo.setModifyUserId(registerRpcVo.getId());
        attributeRpcVo.setVersion(attributeRpcVo.getVersion()==null ? 0 : attributeRpcVo.getVersion()+1);
        attributeRpcVo.setUserEnterpriseAttributeId(registerRpcVo.getEnterpriseUserRpcVo().getId());

        Integer agentCount = agent.getAgentCount()==null ? 0 : agent.getAgentCount();
        Integer forwardCount = agent.getForwardCount()==null ? 0 : agent.getForwardCount();
        Integer personalAgentCount = agent.getPersonalAgentCount()==null ? 0 : agent.getPersonalAgentCount();
        Integer enterpriseAgentCount = agent.getEnterpriseAgentCount()==null ? 0 : agent.getEnterpriseAgentCount();

        attributeRpcVo.setAgentCount(attributeRpcVo.getAgentCount()==null ? agentCount : agentCount+attributeRpcVo.getAgentCount());
        attributeRpcVo.setForwardCount(attributeRpcVo.getForwardCount()==null ? forwardCount : forwardCount+attributeRpcVo.getForwardCount());
        attributeRpcVo.setPersonalAgentCount(attributeRpcVo.getPersonalAgentCount()==null ? personalAgentCount : personalAgentCount+attributeRpcVo.getPersonalAgentCount());
        attributeRpcVo.setEnterpriseAgentCount(attributeRpcVo.getEnterpriseAgentCount()==null ? enterpriseAgentCount : enterpriseAgentCount+attributeRpcVo.getEnterpriseAgentCount());

        registerRpcVo.getEnterpriseUserRpcVo().setEnterpriseAgentAttributeRpcVo(attributeRpcVo);
        cacheService.putObjectPermanent(loginKey, registerRpcVo);

        OjUserEnterpriseAgentAttribute agentAttribute = new OjUserEnterpriseAgentAttribute();
        Utils.copyNotNullObject(attributeRpcVo,agentAttribute);
        //mq 持久化
        mqSender.send(MqKeyConstants.updateEnterpriseAgent, agentAttribute);
    }

    @Override
    public void updateUserById(UserRpcVo userRpcVo, String phone, String userType) {
        String loginKey = RedisKeyConstants.getUserLoginKey(phone, userType);
        if (!cacheService.existsKey(loginKey)){
            throw new DataUpdateException("用户不存在");
        }
        UserRegisterRpcVo registerRpcVo = cacheService.getObject(loginKey, UserRegisterRpcVo.class);
        userRpcVo.setId(registerRpcVo.getId());
        userRpcVo.setModifyUserId(registerRpcVo.getId());
        registerRpcVo.setUsername(org.apache.commons.lang3.StringUtils.isEmpty(userRpcVo.getUsername()) ? registerRpcVo.getUsername() : userRpcVo.getUsername());
        registerRpcVo.setPhone(org.apache.commons.lang3.StringUtils.isEmpty(userRpcVo.getPhone()) ? registerRpcVo.getPhone() : userRpcVo.getPhone());
        registerRpcVo.setEmail(org.apache.commons.lang3.StringUtils.isEmpty(userRpcVo.getEmail()) ? registerRpcVo.getEmail() : userRpcVo.getEmail());
        registerRpcVo.setWeChat(org.apache.commons.lang3.StringUtils.isEmpty(userRpcVo.getWeChat()) ? registerRpcVo.getWeChat() : userRpcVo.getWeChat());
        registerRpcVo.setOicq(org.apache.commons.lang3.StringUtils.isEmpty(userRpcVo.getOicq()) ? registerRpcVo.getOicq() : userRpcVo.getOicq());
        registerRpcVo.setPassword(org.apache.commons.lang3.StringUtils.isEmpty(userRpcVo.getPassword()) ? registerRpcVo.getPassword() : Utils.encryptPassword(userRpcVo.getPassword().trim(), credentialsMatcher.getPasswordService()));
        registerRpcVo.setStatus(userRpcVo.getStatus()==null ? registerRpcVo.getStatus() : userRpcVo.getStatus());
        registerRpcVo.setAuditStatus(userRpcVo.getAuditStatus()==null ? registerRpcVo.getAuditStatus() : userRpcVo.getAuditStatus());
        registerRpcVo.setMessageSendIdentify(org.apache.commons.lang3.StringUtils.isEmpty(userRpcVo.getMessageSendIdentify()) ? registerRpcVo.getMessageSendIdentify() : userRpcVo.getMessageSendIdentify());
        registerRpcVo.setOpenId(org.apache.commons.lang3.StringUtils.isEmpty(userRpcVo.getOpenId()) ? registerRpcVo.getOpenId() : userRpcVo.getOpenId());
        //redis 更新用户信息
        cacheService.putObjectPermanent(loginKey, registerRpcVo);
        //mq 持久化
        OjUser user = new OjUser();
        Utils.copyNotNullObject(userRpcVo, user);
        mqSender.send(MqKeyConstants.updateUser,user);
    }


}
